package com.jiaai.util.rongyun;

import cn.jiguang.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.jiaai.util.ThreadPoolProxy;
import com.jiaai.util.rongyun.msgtype.MsgExtra;
import com.jiaai.util.rongyun.rong.RongCloud;
import com.jiaai.util.rongyun.rong.messages.TxtMessage;
import com.jiaai.util.rongyun.rong.models.CodeSuccessResult;
import com.jiaai.util.rongyun.rong.models.ListGagChatroomUserResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * class_name:RongYunUtil
 * describe:------------------>融云工具
 * create_user:张昉平
 * create_date:2018/1/19
 * create_time:9:08
 */
public class RongYunUtil {

    private static final Logger xlog = LoggerFactory.getLogger(RongYunUtil.class);

    private static   RongYunUtil rongYunUtil;
    private static RongCloud rongCloud;
    private static ThreadPoolProxy poolProxy;
    public  void init(String ak, String as,ThreadPoolProxy threadPoolProxy){
        rongCloud = RongCloud.getInstance(ak, as);
        poolProxy=threadPoolProxy;
    }

    public static synchronized RongYunUtil getInstance(){
        if(rongYunUtil==null){
            rongYunUtil=new RongYunUtil();
        }
        return rongYunUtil;
    }

    public static void main(String[] args) {
//        FamilyJoinMsg msg = new FamilyJoinMsg();
//        msg.setFamilyHeadImg("头像");
//        msg.setFamilyId(1);
//        msg.setFamilyName("name");
//
//        MsgExtra extra = new MsgExtra();
//        extra.setUserId("23211140344");
//        extra.setType(RYMsgType.INVITE_JOIN_FAMILY);
//
//        sendMsgPrivate("8brlm7uf8tmp3","xRiZAiNHGv", JSONObject.toJSONString(msg), extra);

    }

    //私聊
    /**

     * @param content 内容
     * @param msgExtra 额外字段
     */
    public   void sendMsgPrivate(final String content, final String toUserId , final MsgExtra msgExtra){
        if(rongCloud==null){
            xlog.error("请初调用init()方法完成初始化");
            return;
        }
        poolProxy.commitTask(new Runnable() {
            public void run() {
                TxtMessage messagePublishChatroomTxtMessage = new TxtMessage( content, JSONObject.toJSONString(msgExtra));
                try {
                    CodeSuccessResult res = rongCloud.message.publishPrivate("admin", new String[]{toUserId}, messagePublishChatroomTxtMessage, "", "", "", 0, 0, 0, 0);
                    xlog.error(res.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
    //聊天室消息
    /**

     * @param toUserId
     * @param content 内容
     * @param msgExtra 额外字段
     */
    public  void sendMsgPublic(final String content, final String toUserId, final MsgExtra msgExtra){
        if(rongCloud==null){
            xlog.error("请初调用init()方法完成初始化");
            return;
        }
        poolProxy.commitTask(new Runnable() {
            public void run() {
                TxtMessage messagePublishChatroomTxtMessage = new TxtMessage(
                        content
                        , JSONObject.toJSONString(msgExtra));
                try {
                    CodeSuccessResult res = rongCloud.message.publishChatroom("admin", new String[]{toUserId}, messagePublishChatroomTxtMessage);
                    xlog.error(res.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
    //聊天室消息, 自定义发送人
    /**
     * @param fromUser 自定义发送人

     * @param content 内容
     * @param msgExtra 额外字段 里面有userId
     */
    public  void sendMsgPublic(final String fromUser, final String toUser, final String content, final MsgExtra msgExtra){
        if(rongCloud==null){
            xlog.error("请初调用init()方法完成初始化");
            return;
        }
        poolProxy.commitTask(new Runnable() {
            public void run() {
                TxtMessage messagePublishChatroomTxtMessage = new TxtMessage(
                        content
                        , JSONObject.toJSONString(msgExtra));
                try {
                    CodeSuccessResult res = rongCloud.message.publishChatroom(fromUser, new String[]{toUser}, messagePublishChatroomTxtMessage);
                    xlog.error(res.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
    //聊天室消息
    /**

     * @param msg TxtMessage
     * @param userId
     */
    public  void sendMsgPublic(final TxtMessage msg, final String userId){
        if(rongCloud==null){
            xlog.error("请初调用init()方法完成初始化");
            return;
        }
        poolProxy.commitTask(new Runnable() {
            public void run() {
                try {
                    CodeSuccessResult res = rongCloud.message.publishChatroom("admin", new String[]{userId}, msg);
                    xlog.error(res.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
    //禁言
    public  void addGadUser(final String userId, final String chatRoomId){
        poolProxy.commitTask(new Runnable() {
            public void run() {
                CodeSuccessResult groupAddGagUserResult = null;
                try {
                    groupAddGagUserResult = rongCloud.chatroom.addGagUser(userId, chatRoomId, "60");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                xlog.error("addGagUser:  " + groupAddGagUserResult.toString());
            }
        });

    }
    //获取禁用列表
    public  void getGadUser(final String chatRoomId){
        poolProxy.commitTask(new Runnable() {
            public void run() {
                ListGagChatroomUserResult chatroomListGagUserResult = null;
                try {
                    chatroomListGagUserResult = rongCloud.chatroom.ListGagUser(chatRoomId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("ListGagUser:  " + chatroomListGagUserResult.toString());
            }
        });

    }
}
